import express from "express"
import userApi from "./api/userApi"
import questionApi from "./api/questionApi"
import testPaperApi from "./api/testPaperApi"
import studentApi from "./api/studnetApi"

import errorMiddleware from "./errorMiddleware"
import ResponseHelper from "./ResponseHelper"
import ResultHelper from "../utils/ResultHelper"

const PORT = process.env.PORT

const app = express()

// 解析 application/x-www-form-urlencoded 格式的请求体
app.use(express.urlencoded({ extended: true }))

// 解析 application/json 格式的请求体
app.use(express.json())


// app.all('*', function (req, res, next) {
//   res.header('Access-Control-Allow-Origin', req.headers.origin);//获取请求源 这样所有请求就都有访问权限了
//   // res.header('Access-Control-Allow-Credentials', true);
//   res.header('Access-Control-Allow-Headers', 'Content-Type,Content-Length, Authorization, Accept,X-Requested-With')
//   res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS');
//   res.header('Content-Type', 'application/json;charset=utf-8');
//   next()
// });

app.get('/', function (req, res) {
  res.send('Hello World')
})

// 用户API
app.use('/api/user', userApi)

// 题目API
app.use('/api/question', questionApi)

app.use('/api/testPaper', testPaperApi)

app.use('/api/student', studentApi)

app.get('/api/ping', (req, res, next) => {
  const result = ResultHelper.success({}, '测试接口，服务正常')
  ResponseHelper.send(result, res)
})

app.use(errorMiddleware)

app.listen(PORT, () => {

  console.log(`listen port 3001`);
})